package org.glut.friendsService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.glut.friendsService.service.IMessagesService;
import org.glut.friendsService.entity.Messages;
import org.glut.friendsService.mapper.MessagesMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务实现类
 *
 * @author qjc
 * @since 2024-08-16 12:32
 */
@Service
public class MessagesServiceImpl extends ServiceImpl<MessagesMapper, Messages> implements IMessagesService {

    @Autowired
    private MessagesMapper messageMapper;

    @Override
    public boolean sendMessage(Integer senderId, Integer receiverId, String content) {
        Messages message = new Messages();
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        message.setContent(content);
        return messageMapper.insert(message) == 1;
    }

    @Override
    public List<Messages> getMessages(Integer senderId, Integer receiverId) {
        QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sender_id", senderId).eq("receiver_id", receiverId)
                .or().eq("sender_id", receiverId).eq("receiver_id", senderId)
                .orderByAsc("send_time");
        return messageMapper.selectList(queryWrapper);
    }
}
